
package com.oa.modules.sys.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.oa.modules.sys.entity.SysUserEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author admin
 */
@Mapper
public interface SysUserDao extends BaseMapper<SysUserEntity> {

    /**
     * 查询用户的所有权限
     *
     * @param userId 用户ID
     */
    List<String> queryAllPerms(Long userId);

    /**
     * 查询用户的所有菜单ID
     */
    List<Long> queryAllMenuId(Long userId);

    /**
     * 根据用户名，查询系统用户
     */
    SysUserEntity queryByUserName(String username);

    /**
     * 根据条件查询审核人或抄送人列表
     *
     * @param params
     * @return
     */
    List<SysUserEntity> auditUserList(IPage<SysUserEntity> page, @Param("params") Map<String, Object> params);

	long auditUserListCount(@Param("params") Map<String,Object> params);

    /**
     * 验证用户权限
     *
     * @param user
     * @return
     */
    List<Map<String, Object>> validatorUserRole(SysUserEntity user);

    /**
     * 查询总条数
     *
     * @param page
     * @param params
     * @return
     */
	@Select("<script>" +
			"SELECT COUNT( 1 ) FROM " +
			"(select " +
			"t1.* from sys_user t1 " +
			"LEFT JOIN sys_dept t2 on t1.dept_id = t2.dept_id " +
			"where 1 = 1 " +
			"<when test='params.tenantId!=null'> and t2.TENANT_ID = #{params.tenantId} </when>" +
			"<when test='params.createUserId!=null'> and t1.create_user_id = #{params.createUserId} </when>" +
			"<when test='params.username!=null'> and t1.`name` LIKE '%${params.username}%' </when>" +
			"<if test=\"params.username!=null and params.username!=''\">" +
			"   t1.username like concat('%',#{params.username},'%') " +
			"</if>" +
			"order by t1.create_time desc)" +
			"TOTAL" +
			"</script>")
    long findPage(IPage<SysUserEntity> page, Map<String, Object> params);

    /**
     * 查询分页数据
     *
     * @param page
     * @param params
     * @return
     */
    @Select("<script>" +
			"select " +
			"t1.* " +
			"from sys_user t1 " +
			"LEFT JOIN sys_dept t2 on t1.dept_id = t2.dept_id " +
			"where 1 = 1 " +
			"<when test='params.tenantId!=null'> and t2.TENANT_ID = #{params.tenantId} </when>" +
			"<when test='params.createUserId!=null'> and t1.create_user_id = #{params.createUserId} </when>" +
			"<when test='params.username!=null'> and t1.`name` LIKE '%${params.username}%' </when>"+
			"<if test=\"params.username!=null and params.username!=''\">" +
			"   t1.username like concat('%',#{params.username},'%') " +
			"</if>" +
			"order by t1.create_time desc" +
			"</script>")
    List<SysUserEntity> findRecords(IPage<SysUserEntity> page, Map<String, Object> params);

	/**
	 * 查询某个身份下的用户列表
	 *
	 * @return
	 */
	@Select("<script>" +
			"select " +
			"t1.* " +
			"from sys_user t1 " +
			"LEFT JOIN sys_user_role t2 on t1.user_id = t2.user_id " +
			"where 1 = 1 " +
		    "<if test='roleId != null'> and t2.role_id = ${roleId} </if> " +
			"order by t1.create_time desc" +
			"</script>")
	List<SysUserEntity> getOperator(@Param("roleId") String roleId);

	/**
	 * @return java.util.List
	 * @Description 根据openid获取
	 * @Date 15:47 2020/3/10 0010
	 * @Param openId
	 **/
	@Select("<script> " +
			"select a.*,b.bank_name " +
			" FROM sys_user a left join user_bank b on a.user_id = b.userid" +
			" WHERE 1=1 " +
			" and a.wx_open_id = #{openId}" +
			"</script>")
	List<SysUserEntity> qryByOpenId(@Param("openId") String openId);
}
